﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace CamHelper.Interpreter
{
    public class Parameter : IParameter
    {
        public IParameterFileLine Line { get; private set; }
        public IParameter BaseEntry { get; set; }
        public string UniqueKey { get; private set; }
        public string ReadableKey { get; private set; }

        public string Category { get; private set; }
        public string Key { get; private set; }
        public string Value { get; set; }

        public Parameter(IParameterFileLine line)
            : this(line, null)
        { }

        public Parameter(IParameterFileLine line, IParameter baseEntry)
        {
            Line = line;
            BaseEntry = baseEntry;

            Category = Line.Category;
            Key = Line.Key;
            Value = Line.Value;

            Initialize();
        }

        public Parameter(string category, string key, string value)
            : this(category, key, value, null)
        { }

        public Parameter(string category, string key, string value, IParameter baseEntry)
        {
            Line = null;
            BaseEntry = baseEntry;

            Category = category;
            Key = key;
            Value = value;

            Initialize();
        }

        public Parameter(string uniqueKey)
        {
            if (uniqueKey[0] == '.')
                uniqueKey = uniqueKey.Substring(1);

            var pos = uniqueKey.IndexOf('.');

            if (pos >= 0)
            {
                Category = uniqueKey.Substring(0, pos);
                Key = uniqueKey.Substring(pos + 1);
            }
            else
            {
                Category = String.Empty;
                Key = uniqueKey;
            }
            Initialize();
        }

        private void Initialize()
        {
            Category = PascalCase(Category);
            Key = PascalCase(Key);
            var category = String.Empty;
            var key = String.Empty;

            if (Line == null)
            {
                category = Category.ToLower();
                key = Key.ToLower();
            }
            else
            {
                category = Line.CategoryLower;
                key = Line.KeyLower;
            }

            if (category.Length > 0)
            {
                if (category == "par" || category == "global")
                {
                    UniqueKey = String.Format(".{0}.{1}", category, key);
                    ReadableKey = String.Format(".{0}.{1}", Category, Key);
                }
                else
                {
                    UniqueKey = String.Format("{0}.{1}", category, key);
                    ReadableKey = String.Format("{0}.{1}", Category, Key);
                }
            }
            else
            {
                UniqueKey = key;
                ReadableKey = Key;
            }

        }

        private string PascalCase(string s)
        {
            if (s == null || s.Length == 0)
                return String.Empty;

            var array = s.Split('_');

            for (int i = 0; i < array.Length; i++)
            {
                var tmp = array[i];
                array[i] = Char.ToUpper(tmp[0]) + tmp.Substring(1);
            }

            return String.Join("_", array);
        }

        public override string ToString()
        {
            string s = String.Format("{0} = {1}", ReadableKey, Value);
            return s;
        }
    }
}
